import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_base_kit/home/video/controller/video_list_controller.dart';
import 'package:get/get.dart';
import 'package:video_player/video_player.dart';

import '../../utils/log_util.dart';
import 'home_list_video_item_widget.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2020/12/14.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
///代码清单12-9  视频列表页面
class HomeVideoPage extends StatelessWidget {
  //注入控制器
  final VideoListController videoListController =
      Get.put(VideoListController());

  HomeVideoPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey[200],
      body: ListView.custom(
        //缓存空间
        cacheExtent: 0.0,
        //自定义代理
        childrenDelegate: CustomScrollDelegate(
          (BuildContext context, int index) {
            //构建子Itme显示布局
            return ListViewItemWidget(widgetIndex: index);
          },
          itemCount: 1000, //子Item个数
          //滑动回调
          scrollCallBack: (int firstIndex, int lastIndex) {
            print("firstIndex $firstIndex lastIndex $lastIndex");
            //播放的视频Item滑动视图时需要关闭视频播放
            videoListController.checkBorderLimit(
              firstIndex,
              lastIndex,
            );
          },
        ),
      ),
    );
  }
}

///代码清单 5-4-5  ListView 自定义滑动监听回调
///lib/code5/example_510_ListView.dart
class CustomScrollDelegate extends SliverChildBuilderDelegate {
  //定义滑动回调监听
  Function(int firstIndex, int lastIndex) scrollCallBack;

  //构造函数
  CustomScrollDelegate(builder,
      {required int itemCount, required this.scrollCallBack})
      : super(builder, childCount: itemCount);

  @override
  double? estimateMaxScrollOffset(int firstIndex, int lastIndex,
      double leadingScrollOffset, double trailingScrollOffset) {
    scrollCallBack(firstIndex, lastIndex);
    return super.estimateMaxScrollOffset(
        firstIndex, lastIndex, leadingScrollOffset, trailingScrollOffset);
  }
}
